package com.jinlejia.erp.ui.house;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentHouseBinding;
import com.jinlejia.erp.ui.common.GuideDialog;
import com.jinlejia.erp.ui.house.prospect.SearchCoreSellActivity;
import com.jinlejia.erp.utils.ACache;

import static android.app.Activity.RESULT_OK;

public class HouseFragment extends Fragment {

    private View view;
    private int segmentIndex = 0;

    private FragmentHouseBinding binding;

    private String easte;
    private String easteCode;

    private String easte1;
    private String easteCode1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_house, null);
            binding = DataBindingUtil.bind(view);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (segmentIndex == 0)
            ft.replace(R.id.content, new SellingFragment());
        else if (segmentIndex == 1)
            ft.replace(R.id.content, new RentFragment());
        ft.commitAllowingStateLoss();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(ACache.get(getActivity()).getAsString("house_type_guide"))) {
            GuideDialog dialog = new GuideDialog(getContext(), "house_type_guide", R.mipmap.guide_house_type);
            dialog.show();
            dialog.setDissmissListener(() -> {
                if (!TextUtils.isEmpty(ACache.get(getActivity()).getAsString("house_type_guide")) &&
                        TextUtils.isEmpty(ACache.get(getActivity()).getAsString("house_enter_guide")))
                    new GuideDialog(getContext(), "house_enter_guide", R.mipmap.guide_house_enter).show();
            });
        }
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                Intent intent = new Intent(getActivity(), HouseRecordAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_search:
                Intent intent1 = new Intent(getActivity(), SearchCoreSellActivity.class);
                switch (segmentIndex) {
                    case 0:
                        // 出售
                        intent1.putExtra("type", "shs");
                        intent1.putExtra("easte", easte);
                        intent1.putExtra("easteCode", easteCode);
                        startActivityForResult(intent1, 100);
                        break;
                    case 1:
                        //出租
                        intent1.putExtra("type", "rent");
                        intent1.putExtra("easte", easte1);
                        intent1.putExtra("easteCode", easteCode1);
                        startActivityForResult(intent1, 200);
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }

    private void initView() {
        binding.btAdd.setOnClickListener(this::onClick);
        binding.btSearch.setOnClickListener(this::onClick);

        binding.segmentControl.setOnSegmentControlClickListener(index -> {
            segmentIndex = index;
            FragmentManager fm = getChildFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            switch (index) {
                case 0:
                    ft.replace(R.id.content, new SellingFragment());
                    ft.commitAllowingStateLoss();
                    break;
                case 1:
                    ft.replace(R.id.content, new RentFragment());
                    ft.commitAllowingStateLoss();
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 100) {
            easte = data.getStringExtra("easte");
            easteCode = data.getStringExtra("easteCode");
        }else if (resultCode == RESULT_OK && requestCode == 200){
            easte1 = data.getStringExtra("easte");
            easteCode1 = data.getStringExtra("easteCode");
        }
    }
}
